package com.octopus.kernal.ui.element;

import java.lang.reflect.InvocationTargetException;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

import org.apache.commons.beanutils.BeanUtils;

@SuppressWarnings("serial")
@XmlType(name="InputElement")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class InputElement extends BaseElement{
	
	@XmlAttribute
	private String type = "text";
	
	@XmlAttribute
	private String datatype = "String";
	
	@XmlAttribute
	private Integer size;
	
	@XmlAttribute
	private Integer maxlength;
	
	@Override
	public String toString(){
		StringBuffer str = new StringBuffer();
		StringBuffer strhidden = new StringBuffer();
		if(this.isReadonly()){
			if(getValue() != null)
				str.append(getTranslationValue(getValue().toString()));
		}else{
			str.append("<input type=\"" + type + "\" ");
			strhidden.append("<input type=\"hidden\" ");
			if(this.getId() != null){
				str.append("id=\"" + this.getId() + "\" ");
				strhidden.append("id=\"" + this.getId() + "\" ");
			}
			if(this.getName() != null){
				str.append("name=\"" + this.getName() + "\" ");
				strhidden.append("name=\"" + this.getName() + "\" ");
			}
			if(this.getValue() != null){
				str.append("value=\"" + this.getValue().toString() + "\" ");
				strhidden.append("value=\"" + this.getValue().toString() + "\" ");
			}
			if(this.getSize() != null)
				str.append("size=\"" + this.getSize().toString() + "\" ");
			if(this.getOnfocus() != null)
				str.append("onfocus=\"" + this.getOnfocus().toString() + "\" ");
			if(this.getCssclass() != null)
				str.append("class=\"" + this.getCssclass().toString() + "\" ");
			if(this.getStyle() != null)
				str.append("style=\"" + this.getStyle().toString() + "\" ");
			if(this.isDisabled())
				str.append("disabled=\"disabled\" ");
			str.append(">");
			strhidden.append(">");
			if(this.isDisabled() && this.getValue() != null)
				str.append(strhidden.toString());
		}
		return str.toString();
	}
	
	public InputElement clone(){
		InputElement result = new InputElement();
		try {
			BeanUtils.copyProperties(result, this);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return result;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public Integer getSize() {
		return size;
	}

	public void setSize(Integer size) {
		this.size = size;
	}

	public Integer getMaxlength() {
		return maxlength;
	}

	public void setMaxlength(Integer maxlength) {
		this.maxlength = maxlength;
	}

	public String getDatatype() {
		return datatype;
	}

	public void setDatatype(String datatype) {
		this.datatype = datatype;
	}

}
